package dht

import (
	"errors"
	"net"
)

type DHTConfig struct {
	ID     ID
	IP     string
	Port   uint16
	Seeds  []string
	DBType string
	DBPath string
}

func GenerateDHTConfig() (*DHTConfig, error) {
	var err error
	dc := &DHTConfig{}
	dc.ID, err = GenerateID()
	if err != nil {
		return nil, err
	}
	dc.IP = "0.0.0.0"
	dc.Port = 0
	dc.DBType = "memory"
	return dc, nil
}

func (dc *DHTConfig) Check() error {
	if dc == nil {
		return errors.New("DHTConfig Not Init")
	}
	if net.ParseIP(dc.IP) == nil {
		return errors.New("DHTConfig IP Format Error")
	}
	exists := false
	for _, v := range []string{"memory", "bolt"} {
		if dc.DBType == v {
			exists = true
			break
		}
	}
	if exists == false {
		return errors.New("DHTConfig No Support DB Type")
	}
	return nil
}
